//
//  ZNGSearchHistoryManager.m
//  开店通
//
//  Created by 张浩 on 15/9/6.
//  Copyright (c) 2015年 张浩. All rights reserved.
//

#import "ZNGSearchHistoryManager.h"
#import "ZNGConstants.h"
#import "MJExtension.h"
#define kSearchHistoryList                  [kDocPath stringByAppendingPathComponent:@"history.data"]

@implementation ZNGSearchHistoryManager
+ (NSArray *)searchHistory
{
    ZNGSearchHistory *history = [NSKeyedUnarchiver unarchiveObjectWithFile:kSearchHistoryList];
    return history.histories;
}

+ (void)saveSearchHistory:(NSArray *)searchHistory
{
    ZNGSearchHistory *history = [NSKeyedUnarchiver unarchiveObjectWithFile:kSearchHistoryList];
    if (history == nil) {
        history = [[ZNGSearchHistory alloc] init];
    }
    history.histories = searchHistory;
    [NSKeyedArchiver archiveRootObject:history toFile:kSearchHistoryList];
}

+ (void)addASearchRecord:(NSString *)searchWord
{
    NSMutableArray *history = [NSMutableArray arrayWithArray:[self searchHistory]];
    
    for (NSString *str in history) {
        if ([searchWord isEqualToString:str]) {
            [history removeObject:str];
            break ;
        }
    }
    [history insertObject:searchWord atIndex:0];
    [self saveSearchHistory:history];
}

+ (void)clearAllSearchHistory
{
    ZNGSearchHistory *history = [NSKeyedUnarchiver unarchiveObjectWithFile:kSearchHistoryList];
    history.histories = nil;
    [NSKeyedArchiver archiveRootObject:history toFile:kSearchHistoryList];
}
@end

@implementation ZNGSearchHistory
MJCodingImplementation
@end